package com.senseuni.da.db.dao

import androidx.room.Dao
import androidx.room.Insert
import androidx.room.Query
import com.senseuni.da.db.entity.AppLogEntity

@Dao
interface AppLogDao {

    @Insert
    fun insert(entity: AppLogEntity) : Long

    @Insert
    fun insert(entities: List<AppLogEntity>)

    @Query("UPDATE app_log SET status=:status WHERE id IN (:ids)")
    fun updateStatus(ids: List<Long>, status: Int): Int

    @Query("UPDATE app_log SET status=2,upload_time=:time WHERE id IN (:ids)")
    fun updateSuccess(ids: List<Long>, time: Long): Int

    @Query("UPDATE app_log SET status=0,msg=:msg,retry_times=retry_times+1 WHERE id IN (:ids)")
    fun updateFail(ids: List<Long>, msg:String): Int

//    @Query("SELECT * from adv_data order by id asc")
//    fun findAll() : Observable<List<AdvDataEntity>>

    @Query("SELECT * FROM app_log WHERE status = 0 and retry_times<10 order by id asc LIMIT 10")
    fun findLogs(): List<AppLogEntity>

    @Query("SELECT * from app_log order by id asc")
    fun findAll() : List<AppLogEntity>?

    @Query("DELETE FROM app_log")
    fun deleteAll()

    @Query("SELECT COUNT(*) FROM app_log WHERE status = 0 and retry_times < 10")
    fun countByNeedUpload() : Int

    @Query("DELETE FROM app_log WHERE status = 2 and create_time < :time")
    fun deleteOldData(time: Long)

}